<?php
class TPLAdminQuestion
{
	public static function Menu() {
		$lang=Eleanor::$Language[$GLOBALS['Eleanor']->module['config']['n']];
		$links=&$GLOBALS['Eleanor']->module['links'];

		$categs=isset($GLOBALS['Eleanor']->module['navigation']['categories']) ? $GLOBALS['Eleanor']->module['navigation']['categories'] : false;
		$options=isset($GLOBALS['Eleanor']->module['navigation']['options']) ? $GLOBALS['Eleanor']->module['navigation']['options'] : false;

		$GLOBALS['Eleanor']->module['navigation']=array(
			array($links['list'],$lang['list'],'act'=>$act=='list',
				'submenu'=>array(
					$links['newlist'] ? array($links['newlist']['link'],sprintf($lang['news'],$links['links']['cnt']),'act'=>false) : false,
					array($links['add'],$lang['add'],'act'=>$act=='add'),
				),
			),
			$options ? $options : array($links['options'],Eleanor::$Language['main']['options'],'act'=>$act=='options'),
			$categs ? $categs : array($links['categories'],$lang['cats_manage']),
			//array($links['addf'],$lang['addf'],'act'=>$act=='addf'),
		);
	}

	public static function QuestionList($items,$categs,$cnt,$pp,$qs,$page,$links)
	{
		static::Menu();
		$GLOBALS['jscripts'][]='js/checkboxes.js';
		$lang=Eleanor::$Language[$GLOBALS['Eleanor']->module['config']['n']];
		$ltpl=Eleanor::$Language['tpl'];

		$qs+=array(''=>array());
		$qs['']+=array('fi'=>array());
		$fs=(bool)$qs['']['fi'];
		$qs['']['fi']+=array(
			'title'=>false,
			'titlet'=>false,
			'status'=>false,
			'category'=>false,
		);

		$Lst=Eleanor::LoadListTemplate('table-list',6)->begin(
			array($ltpl['title'],'sort'=>$qs['sort']=='title' ? $qs['so'] : false,'href'=>$links['sort_title']),
			$lang['category'],
			array($lang['date'],'sort'=>$qs['sort']=='date' ? $qs['so'] : false,'href'=>$links['sort_date']),
			array($lang['author'],'sort'=>$qs['sort']=='author' ? $qs['so'] : false,'href'=>$links['sort_author']),
			array($ltpl['functs'],80,'sort'=>$qs['sort']=='id' ? $qs['so'] : false,'href'=>$links['sort_id']),
			array(Eleanor::Check('mass',false,array('id'=>'mass-check')),20)
		);
		if($items) {
			$images=Eleanor::$Template->default['theme'].'images/';
			foreach($items as $k => $v) {
				$cats='';
				foreach($v['cats'] as &$cv)
					if(isset($categs[$cv]))
						$cats.=($cats ? '' : '<b>').$categs[$cv]['title'].($cats ? '' : '</b>').', ';
				$Lst->item(
					'<a id="it'.$k.'" href="'.$v['_aedit'].'">'.$v['title'].'</a>',
					$cats ? rtrim($cats,', ') : array('--','center'),
					array($v['date'],'center'),
					$v['author_id'] ? '<a href="'.Eleanor::$Login->UserLink(htmlspecialchars_decode($v['author'],ELENT),$v['author_id']).'">'.$v['author'].'</a>' : $v['author'],
					$Lst('func',
						$v['_aswap'] ? array($v['_aswap'],$v['status']<=0 ? $ltpl['activate'] : $ltpl['deactivate'],$v['status']<0 ? $images.'waiting.png' : $images.($v['status']==0 ? 'inactive.png' : 'active.png')) : '<img src="'.$images.'inactive.png'.'" alt="" title="'.strip_tags(['endeddate']).'" />',
						array($v['_aedit'],$ltpl['edit'],$images.'edit.png'),
						array($v['_adel'],$ltpl['delete'],$images.'delete.png')
					),
					Eleanor::Check('mass[]',false,array('value'=>$k))
				);
			}
		}
		else
			$Lst->empty($lang['not_found']);

		$fititlet=$statuses='';
		$temp=array(
			'b'=>$lang['begins'],
			'q'=>$lang['match'],
			'e'=>$lang['endings'],
			'm'=>$lang['contains'],
		);
		foreach($temp as $k=>&$v)
			$fititlet.=Eleanor::Option($v,$k,$qs['']['fi']['titlet']==$k);
		$temp=array(
			-1=>$lang['waitmod'],
			0=>$lang['blocked'],
			1=>$lang['active'],
		);
		foreach($temp as $k=>&$v)
			$statuses.=Eleanor::Option($v,$k,$qs['']['fi']['status']!==false and $qs['']['fi']['status']==$k);
		return Eleanor::$Template->Cover(
			'<form method="post">
<table class="tabstyle tabform" id="ftable">
	<tr class="infolabel"><td colspan="2"><a href="#">'.$ltpl['filters'].'</a></td></tr>
	<tr>
		<td><b>'.$ltpl['title'].'</b><br />'.Eleanor::Select('fi[titlet]',$fititlet,array('style'=>'width:30%')).Eleanor::Input('fi[title]',$qs['']['fi']['title'],array('style'=>'width:68%')).'</td>
		<td><b>'.$lang['category'].'</b><br />'.Eleanor::Select('fi[category]',Eleanor::Option('&mdash;',0,false,array(),2).Eleanor::Option($lang['nocat'],'no',$qs['']['fi']['category']=='no').$GLOBALS['Eleanor']->Categories->GetOptions($qs['']['fi']['category'])).'</td>
	</tr>
	<tr>
		<td><b>'.$lang['status'].'</b><br />'.Eleanor::Select('fi[status]',Eleanor::Option('&mdash;','-',false,array(),2).$statuses).'</td>
		<td style="text-align:center;vertical-align:middle">'.Eleanor::Button($ltpl['apply']).'</td>
	</tr>
</table>
<script type="text/javascript">//<![CDATA[
$(function(){
	var fitrs=$("#ftable tr:not(.infolabel)");
	$("#ftable .infolabel a").click(function(){
		fitrs.toggle();
		$("#ftable .infolabel a").toggleClass("selected");
		return false;
	})'.($fs ? '' : '.click()').';
	One2AllCheckboxes("#checks-form","#mass-check","[name=\"mass[]\"]",true);
});//]]></script>
		</form><form id="checks-form" action="'.$links['form_items'].'" method="post" onsubmit="return (CheckGroup(this) && confirm(\''.$ltpl['are_you_sure'].'\'))">'
				.$Lst->end().'<div class="submitline" style="text-align:right"><div style="float:left">'.sprintf($lang['tto_pages'],$Lst->perpage($pp,$links['pp'])).'</div>'.$ltpl['with_selected'].Eleanor::Select('op',Eleanor::Option($ltpl['activate'],'a').Eleanor::Option($ltpl['deactivate'],'d').Eleanor::Option($ltpl['delete'],'k').Eleanor::Option($lang['waitmod'],'m')).Eleanor::Button('Ok').'</div></form>'
				.Eleanor::$Template->Pages($cnt,$pp,$page,array($links['pages'],$links['first_page']))
		);
	}

	public static function AddEdit($id,$values,$error,$bypost) {
		$lang=Eleanor::$Language[$GLOBALS['Eleanor']->module['config']['n']];
		if(Eleanor::$vars['multilang']) {
			$mchecks = $ml = array();
			foreach(Eleanor::$langs as $k => &$v) {
				$mchecks[$k]=(!$id or !empty($values['title'][$k]) or !empty($values['text'][$k]) or !empty($values['uri'][$k]));
				$ml['title'][$k]=Eleanor::Input('title['.$k.']',$GLOBALS['Eleanor']->Editor->imgalt=Eleanor::FilterLangValues($values['title'],$k),array('tabindex'=>1,'id'=>'title-'.$k));
				$ml['text'][$k]=$GLOBALS['Eleanor']->Editor->Area('text['.$k.']',Eleanor::FilterLangValues($values['text'],$k),array('bypost'=>$bypost,'no'=>array('tabindex'=>6)));
				$ml['uri'][$k]=Eleanor::Input('url['.$k.']',Eleanor::FilterLangValues($values['uri'],$k),array('onfocus'=>'if(!$(this).val())$(this).val($(\'#title-'.$k.'\').val())','tabindex'=>10));
			}
		} else
			$ml=array(
				'title'=>Eleanor::Input('title',$GLOBALS['Eleanor']->Editor->imgalt=$values['title'],array('tabindex'=>1,'id'=>'title')),
				'text'=>$GLOBALS['Eleanor']->Editor->Area('text',$values['text'],array('bypost'=>$bypost,'no'=>array('tabindex'=>6))),
				'uri'=>Eleanor::Input('uri',$values['uri'],array('onfocus'=>'if(!$(this).val())$(this).val($(\'#title\').val())','tabindex'=>10)),
			);

		$Lst=Eleanor::LoadListTemplate('table-form');
		$c = $Lst->form()
			.$Lst->begin()
			.$Lst->item('Title',Eleanor::$Template->LangEdit($ml['title'],false))
			.($GLOBALS['Eleanor']->Categories->dump
				? $Lst->item($lang['categs'],Eleanor::Items('cats',$GLOBALS['Eleanor']->Categories->GetOptions($values['cats']),array('id'=>'cs','tabindex'=>2))).
					$Lst->item($lang['maincat'],Eleanor::Select('_maincat',$GLOBALS['Eleanor']->Categories->GetOptions($values['_maincat']),array('id'=>'mc','tabindex'=>3)))
				: ''
			)
			.(Eleanor::$vars['multilang'] ? $Lst->item('set_for_langs',Eleanor::$Template->LangChecks($values['one_lang'],$mchecks,null,4)) : '')
			.$Lst->end();

		$text=$Lst->begin()
			.$Lst->item(array($lang['short_text'],Eleanor::$Template->LangEdit($ml['text'],false),'descr'=>$lang['short_text_']))
			.$Lst->item(array($lang['show_sokr'],Eleanor::Check('show_sokr',$values['show_sokr'],array('tabindex'=>7)),'descr'=>$lang['show_sokr_']))
			.$Lst->item(array($lang['show_detail'],Eleanor::Check('show_detail',$values['show_detail'],array('tabindex'=>8)),'descr'=>$lang['show_detail_']))
			.$Lst->item($lang['status'],Eleanor::Select('status',Eleanor::Option($lang['waitmod'],-1,$values['status']==-1).Eleanor::Option($lang['blocked'],0,$values['status']==0).Eleanor::Option($lang['active'],1,$values['status']==1),array('tabindex'=>9)))
			.$Lst->end();

		$extra=$Lst->begin()
			.$Lst->item('uri',Eleanor::$Template->LangEdit($ml['uri'],false))
			.$Lst->item($lang['author'],Eleanor::$Template->Author($values['author'],$values['author_id'],11))
			.$Lst->item(array($lang['pdate'],Dates::Calendar('date',$values['date'],true,array('tabindex'=>12)),'tip'=>$lang['pdate_']))
			.$Lst->item($lang['pinned'],Dates::Calendar('pinned',$values['pinned'],true,array('tabindex'=>13)))
			.$Lst->item($lang['reads'],Eleanor::Input('reads',$values['reads'],array('tabindex'=>14)))
			.($id ? $Lst->item(array($lang['ping'],Eleanor::Check('_ping',$values['_ping'],array('tabindex'=>15)),'descr'=>$lang['ping_'])) : '')
			.$Lst->end();

		$back=isset($_GET['noback']) ? '' : Eleanor::Input('back',isset($_POST['back']) ? (string)$_POST['back'] : getenv('HTTP_REFERER'),array('type' => 'hidden'));
		$c .= $Lst->tabs(
			array('General', $text),
			array('Settings', $extra)
		) . $Lst->submitline($back . Eleanor::Button('Ok','submit',array('tabindex'=>20)).($id ? ' '.Eleanor::Button('Delete','button',array('onclick'=>'window.location=\''.$GLOBALS['Eleanor']->Url->Construct(array('delete'=>$id,'noback'=>1)).'\'')) : ''))
		.$Lst->endform();
		return Eleanor::$Template->Cover($c,$error);
	}

	public static function Categories($c)
	{
		static::Menu();
		return$c;
	}
}
?>